package com.myscreen.client;

import java.awt.*;
import java.awt.image.BufferedImage;  
import java.io.ByteArrayOutputStream;  
import java.io.DataOutputStream;  
import java.io.IOException;  
import java.net.Socket;  
import javax.imageio.ImageIO;  
  
public class ScreenMonitorClient {  
  
    private final String serverHost;  
    private final int serverPort;  
    private Socket socket;  
    private DataOutputStream out;  
  
    public ScreenMonitorClient(String serverHost, int serverPort) {  
        this.serverHost = serverHost;  
        this.serverPort = serverPort;  
    }  
  
    public void connectAndSendScreenshots() {  
        try {  
            socket = new Socket(serverHost, serverPort);  
            out = new DataOutputStream(socket.getOutputStream());  
  
            // 发送注册消息（这里只是一个简单的字符串）  
            //out.writeUTF("REGISTER_CLIENT");
  
            // 模拟定期发送屏幕截图  
            while (true) {
                try {
                    sendScreenCapture();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // 暂停一段时间以模拟屏幕截图的频率  
                Thread.sleep(200); // 每秒发送一次
            }  
        } catch (IOException | InterruptedException e) {  
            e.printStackTrace();  
        } finally {  
            closeResources();  
        }  
    }  
  
    private void sendScreenCapture() throws IOException, AWTException {  
        // 捕获屏幕截图  
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());  
        Robot robot = new Robot();  
        BufferedImage image = robot.createScreenCapture(screenRect);  
  
        // 将截图转换为字节数组  
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
        ImageIO.write(image, "png", byteArrayOutputStream);  
        byte[] imageBytes = byteArrayOutputStream.toByteArray();  
  
        // 发送数据长度  
        out.writeInt(imageBytes.length);  
  
        // 发送数据  
        out.write(imageBytes);  
  
        // 清空缓冲区  
        byteArrayOutputStream.reset();  
    }  
  
    private void closeResources() {  
        if (out != null) {  
            try {  
                out.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        if (socket != null && !socket.isClosed()) {  
            try {  
                socket.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
  
    public static void main(String[] args) {  
        if (args.length < 2) {  
            System.out.println("Usage: java ScreenMonitorClient <serverHost> <serverPort>");  
            System.exit(1);  
        }  
  
        String serverHost = args[0];  
        int serverPort = Integer.parseInt(args[1]);  
  
        ScreenMonitorClient client = new ScreenMonitorClient(serverHost, serverPort);  
        client.connectAndSendScreenshots();  
    }  
}